import React, { useReducer } from 'react'
function reducer(state, action) {
  switch (action.type) {
    case 'add':
        if(state.count >= 30) {
            return { count: state.count+5, suggest: '短袖' }
        }
        if(state.count <= 10) {
            return { count: state.count+5, suggest: '长袖' }
        }
        return { count: state.count+5, suggest: '长袖' }
    case 'minus':
        if(state.count <= 10) {
            return { count: state.count-5, suggest: '长袖' }
        }
        if(state.count >= 30) {
            return { count: state.count-5, suggest: '短袖' }
        }
        return { count: state.count-5, suggest: '长袖' }
    default:
      return state
  }
}
export default function Reducer() {
  const [state, dispatch] = useReducer(reducer, { count: 20, suggest: '长袖' })
  const { count, suggest } = state
 
  return (
    <div>
        <button onClick={() => dispatch({ type: 'add' })}>add</button>
        <button onClick={() => dispatch({ type: 'minus' })}>minus</button>
        <div>
          温度：{count}
        </div>
        <div>
          建议穿衣：{suggest}
        </div>
    </div>
  )
}
